package com.xmotor.controller;

import com.xmotor.common.SysResult;
import com.xmotor.service.TestService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/test")
public class TestController {

    @RequestMapping("/page1")
    @ResponseBody
    public String test1(HttpServletRequest request) {
        String sessionId = request.getSession().getId();
        System.out.println("sessionId: "+sessionId);
        return "test";
    }

    @RequestMapping("/page2")
    public String test2() {
        return "test";
    }

    // 访问：localhost:8080/test/page2

    @Autowired
    private TestService testService;

    @RequestMapping("/upload")
    @ResponseBody
    public SysResult upload(@RequestParam(value = "file") MultipartFile file) {
        String url = testService.uploadFile(file);
        if (StringUtils.isNotBlank(url)) {
            return SysResult.ok(url);
        } else {
            return SysResult.fail("文件上传失败！");
        }
    }


    //jpg--FFD8FFE0
    //png--89504E47
    //bmp--424DF685
    //webp--52494646
    //gif--47494638
    //doc--D0CF11E0
    //xls--D0CF11E0
    //docx--504B0304
    //xlsx--504B0304
    //pdf--25504446
    //mp4--0000001C
    //mp4--00000020
    //flv--464C5601
    //mkv--1A45DFA3
    //wmv--3026B275
    //avi--52494646
    //mov--00000014
    //mpeg(mpg)--000001BA
    //mpeg(mpg)--000001B3
    @RequestMapping("/getFileHeader")
    @ResponseBody
    public SysResult getFileHeader(@RequestParam(value = "file") MultipartFile file) throws Exception {
        String fileHeader = testService.getFileHeader(file);
        return SysResult.ok(fileHeader);
    }

}
